package com.etoak.car.controller;

//import com.etoak.commons.utils.QRCodeUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/er")
public class TestController {

    @GetMapping("/erweima")
    public void erweima() throws Exception {
//        String text = "https://blog.csdn.net/qq_45777315";  //这里设置自定义网站url或文字
//        String logoPath = "D:\\zxc\\1.jpg"; //二维码图片
//        String destPath = "D:\\zxc\\";		//保存地址
//        //调用工具类
//        System.out.println(QRCodeUtils.encode(text, logoPath, destPath, true));
        String content= "https://blog.csdn.net/qq_45777315";  //这里设置自定义网站url或文字
        String path= "D:/zxc/";
        code(content,path);
       // System.out.println();
    }


    public static String code(String content,String path) {
        try {
            String codeName = "331";// 二维码的图片名
            String imageType = "jpg";// 图片类型
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 200, 200, hints);
            File file1 = new File(path, codeName + "." + imageType);
            MatrixToImageWriter.writeToFile(bitMatrix, imageType, file1);
            return codeName+".jpg";
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

}
